import axios from 'axios'
import Mock from 'mockjs'

import Payin from './payinlist'
import User from './user'

const mocks = [
	...Payin,
	...User
]

function XHR2ExpressReqWrap(respond) {
	return function (options) {
		let result = null
		if (respond instanceof Function) {
			const { body, type, url } = options
			result = respond({
				method: type,
				body: JSON.parse(body),
				query: url
			})
		} else {
			result = respond
		}
		return Mock.mock(result)
	}
}

for (const i of mocks) {
	// Mock.mock(url, method, templete) 配置模拟请求: url = 模拟请求路径, method = 模拟请求方法, template = 模拟请求返回
	Mock.mock(i.url, i.type || 'get', XHR2ExpressReqWrap(i.response))
	console.log('mock..')
}

// 模拟请求, 返回并打印结果
axios.get('/data/userList').then(resp => console.log(resp.data))